package action;

import config.ApplicationConfig;
import server.LoginServer;
import server.SubjectServer;
import server.UserServer;
import util.Encode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.LinkedHashMap;

/**
 * User: tum0r
 * Package Name: ${PACKAGE_NAME}
 * Time: 2019-04-20 Saturday 21:35:04
 */
public class ChangeSubjectServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        LinkedHashMap<String, String> pageConfig = ApplicationConfig.readConfig("page.json");
        request.setCharacterEncoding(pageConfig.get("encode"));
        response.setCharacterEncoding(pageConfig.get("encode"));

        HttpSession session = request.getSession();
        String username = session.getAttribute("user") == null ? "" : session.getAttribute("user").toString();
        String cookie = session.getAttribute("cookie") == null ? "" : session.getAttribute("cookie").toString();
        String page = "information.jsp";
        if (username == null || username.equals("") || cookie == null || cookie.equals("")) {
            session.setAttribute("infoTitle", "未登录");
            session.setAttribute("infoContent", "您还未登录，请先登录");
            session.setAttribute("return", "index.jsp");
            session.setAttribute("panelColor", "red");
            session.removeAttribute("user");
            session.removeAttribute("cookie");
        } else {
            LoginServer loginServer = new LoginServer();
            loginServer.open();
            if (!loginServer.checkLogin(username, cookie)) {
                String uuid = request.getParameter("uuid");
                String title = request.getParameter("title");
                String content = request.getParameter("content");
                String format = request.getParameter("format");
                String testCase = request.getParameter("testCase");
                String testCaseResult = request.getParameter("testCaseResult");
                String cppTimeLimit = request.getParameter("cppTimeLimit");
                String cppMemoryLimit = request.getParameter("cppMemoryLimit");
                String javaTimeLimit = request.getParameter("javaTimeLimit");
                String javaMemoryLimit = request.getParameter("javaMemoryLimit");
                String pythonTimeLimit = request.getParameter("pythonTimeLimit");
                String pythonMemoryLimit = request.getParameter("pythonMemoryLimit");
                if (uuid != null && title != null && content != null && format != null && testCase != null &&
                        testCaseResult != null && cppTimeLimit != null && cppMemoryLimit != null &&
                        javaTimeLimit != null && javaMemoryLimit != null && pythonTimeLimit != null
                        && pythonMemoryLimit != null) {
                    Encode encode = new Encode();
                    title = encode.languageEncode(title);
                    content = encode.languageEncode(content);
                    format = encode.languageEncode(format);

                    UserServer userServer = new UserServer();
                    userServer.open();

                    int canChange = userServer.canChange(username, "");

                    userServer.close();

                    switch (canChange) {
                        case 2:
                            SubjectServer subjectServer = new SubjectServer();
                            subjectServer.open();
                            long cpp_t = 1000;
                            long cpp_m = 65535;
                            long java_t = 2000;
                            long java_m = 65535;
                            long python_t = 2000;
                            long python_m = 65535;
                            try {
                                cpp_t = Long.parseLong(cppTimeLimit);
                            } catch (Exception e) {
                            }
                            try {
                                cpp_m = Long.parseLong(cppMemoryLimit);
                            } catch (Exception e) {
                            }
                            try {
                                java_t = Long.parseLong(javaTimeLimit);
                            } catch (Exception e) {
                            }
                            try {
                                java_m = Long.parseLong(javaMemoryLimit);
                            } catch (Exception e) {
                            }
                            try {
                                python_t = Long.parseLong(pythonTimeLimit);
                            } catch (Exception e) {
                            }
                            try {
                                python_m = Long.parseLong(pythonMemoryLimit);
                            } catch (Exception e) {
                            }
                            subjectServer.changeSubject(uuid, title, content, format, testCase, testCaseResult, cpp_t, cpp_m, java_t, java_m, python_t, python_m);
                            subjectServer.close();
                            session.setAttribute("infoTitle", "修改赛题");
                            session.setAttribute("infoContent", "修改成功");
                            session.setAttribute("return", "mine");
                            session.setAttribute("panelColor", "blue");
                            break;
                        default:
                            session.setAttribute("infoTitle", "修改赛题");
                            session.setAttribute("infoContent", "修改失败，你没有权限修改");
                            session.setAttribute("return", "mine");
                            session.setAttribute("panelColor", "red");
                            break;
                    }
                }
            } else {
                session.setAttribute("infoTitle", "未登录");
                session.setAttribute("infoContent", "您还未登录，请先登录");
                session.setAttribute("return", "index.jsp");
                session.setAttribute("panelColor", "red");
                session.removeAttribute("user");
                session.removeAttribute("cookie");
            }
            loginServer.close();
        }
        response.sendRedirect(page);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
